//
//  UIImageView+Catransition.m
//  FeiHuNote
//
//  Created by N-X on 2017/12/7.
//  Copyright © 2017年 MRNAN. All rights reserved.
//  SDWebImage 加载完图片后渐进式显示的动画

#import "UIImageView+Catransition.h"
#import "UIImage+CircleImage.h"

@implementation UIImageView (Catransition)
- (void)nx_setImageWithUrl:(NSString *)url placeHolder:(UIImage *)placeHolder complete:(void (^)(UIImage * image))completion
{
    __weak typeof(self)weakSelf = self;
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeHolder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image && cacheType == SDImageCacheTypeNone) {
            CATransition * transition = [CATransition animation];
            transition.type = kCATransitionFade;
            transition.duration = 0.3;
            transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            [weakSelf.layer addAnimation:transition forKey:nil];
        }
        if (image) {
            completion(image);
        }
    }];
}

- (void)nx_setCircleImageWithUrl:(NSString *)url placeHolder:(UIImage *)placeHolder complete:(void (^)(UIImage *))completion
{
    [self sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
            completion([UIImage circleImage:image]);
        }
    }];
}
@end
